/*
 * Copyright (C) 2011 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.common.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import com.mol.market.Session;

/**
 * 分页动画指示器
 * 
 * @author andrew.wang
 * @date 2011-12-9
 * 
 */
public class IndicatorDrawable extends View {

    /** 指示器直径12 */
    private static final int RADIUS_S = 4;
    
    private static final int HEIGHT = 32;

    private static final int REFRESH = 1;

    /** 分页指示器页数 */
    private int mPageNumber = 0;

    /** 当前的页面 */
    private int mCurrentPage = 0;

    private int mCurrentLevel = 6;
    
    private int mStartPosX = 15;
    private int mStartPosY;

    private Paint mPaint;
    
    private float mDensity;
    
    private int mNormalRadius;
    private int mHighlightStartRadius;
    private int mStepWidth;

    public IndicatorDrawable(Context context) {
        super(context);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }
    
    public IndicatorDrawable(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    public IndicatorDrawable(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }
    
    public void setPageNumber(int page) {
        mDensity = Session.get(getContext()).density;
        mPageNumber = page;
        requestLayout();
        invalidate();
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int height = (int) (HEIGHT * mDensity);
        setMeasuredDimension(mPageNumber * (int) (14 * mDensity), height);
        mStartPosY = height / 2;
        mNormalRadius = (int) (RADIUS_S * mDensity);
        if (mDensity > 1) {
            mHighlightStartRadius = 2;
        } else {
            mHighlightStartRadius = 0;
        }
        mStepWidth = (int) (13 * mDensity);
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int startX = mStartPosX;
        for (int i = 0; i < mPageNumber; i++) {

            if (i == mCurrentPage) {
                mPaint.setColor(Color.WHITE);
                canvas.drawCircle(startX, mStartPosY, mHighlightStartRadius + mCurrentLevel, mPaint);
            } else {
                mPaint.setColor(Color.GRAY);
                canvas.drawCircle(startX, mStartPosY, mNormalRadius, mPaint);
            }
            startX += mStepWidth;
        }
        canvas.save();
    }


    private Handler mHandler = new Handler() {

        public void handleMessage(Message msg) {

            switch (msg.what) {
            case REFRESH:

                mCurrentLevel = msg.arg1;
                invalidate();
                break;

            default:
                break;
            }
        };
    };

    private void startAnimation() {

        new Thread() {
            public void run() {
                for (int i = 1; i <= 6; i++) {
                    Message msg = mHandler.obtainMessage();
                    msg.what = REFRESH;
                    msg.arg1 = i + 1;

                    mHandler.sendMessage(msg);
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    // mHandler.sendMessageDelayed(msg, 20);
                }
            };
        }.start();
    }
    
    public void setPage(int num) {
        
        mCurrentPage = num;
        if (mCurrentPage < 0) {
            mCurrentPage = 0;
            return;
        }else if (mCurrentPage == mPageNumber) {
            mCurrentPage = mPageNumber - 1;
            return;
        }
        mCurrentLevel = 0;
        startAnimation();
    }

    /**
     * 上一页
     */
    public void previousPage() {
        mCurrentPage--;
        if (mCurrentPage < 0) {
            mCurrentPage = 0;
            return;
        }
        mCurrentLevel = 0;
        startAnimation();
    }

    /**
     * 下一页
     */
    public void nextPage() {
        mCurrentPage++;
        if (mCurrentPage == mPageNumber) {
            mCurrentPage = mPageNumber - 1;
            return;
        }
        mCurrentLevel = 0;
        startAnimation();
    }

}
